<template>
<div>
  <el-card>
    <el-row class="addCate">
      <el-col>
        <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
      </el-col>
    </el-row>
    <!-- 表格 -->
    <el-table :data="categoryList" stripe border
              row-key="cat_id">
      <el-table-column type="index" width="60px"></el-table-column>
      <el-table-column label="分类名称" prop="cat_name"></el-table-column>
      <el-table-column label="是否有效" prop="cat_deleted">
        <template v-slot="scope">

          <i class="el-icon-success" style="color: green" v-if="!scope.row.cat_deleted"></i>
          <i class="el-icon-error" style="color: red" v-else></i>
        </template>
      </el-table-column>
      <el-table-column label="排序">
        <template v-slot="scope">
<el-tag v-if="scope.row.cat_level === 0" size="mini">一级</el-tag>
<el-tag type="success" v-else-if="scope.row.cat_level === 1" size="mini">二级</el-tag>
<el-tag type="warning" v-else size="mini">三级</el-tag>

        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button icon="el-icon-edit" type="primary" size="mini" @click="showEditCate(scope.row.cat_name,scope.row.cat_id)">编辑</el-button>
          <el-button icon="el-icon-delete" type="danger" size="mini" @click="removeRolesById(scope.row.cat_id)">删除</el-button>

        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[1, 3, 5, 10]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>


  </el-card>
  <el-dialog title="添加分类" :visible.sync="addCateDialog" @close="dialogClose('addCateDialogRef')">

    <el-form label-width="100px" ref="addCateDialogRef" :rules="addCateRules" :model="addCateForm">
      <el-form-item label="分类名称" prop="cat_name">
        <el-input v-model="addCateForm.cat_name"></el-input>
      </el-form-item>
      <el-form-item label="父分类">
        <el-cascader
            clearable
            v-model="selectedKeys"
            :options="parentCateList"
            :props="cascaderProps"
            @change="parentCateChange" class="CateCascader"></el-cascader>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialog = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
  </el-dialog>
  <el-dialog title="编辑分类" :visible.sync="editCateDialog" @close="dialogClose('editCateDialogRef')">

    <el-form label-width="100px" ref="editCateDialogRef" :rules="addCateRules" :model="addCateForm">
      <el-form-item label="分类名称" prop="cat_name">
        <el-input v-model="addCateForm.cat_name"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="editCateDialog = false">取 消</el-button>
        <el-button type="primary" @click="editCate">确 定</el-button>
      </span>
  </el-dialog>
</div>
</template>

<script>
import {remove} from "@/network/remove";

export default {
  name: "categories",
  data(){
    return{
      categoryList:[],
     queryInfo:{
        type:3,
        pagenum:1,
        pagesize:5
      },
      total:0,
      addCateDialog:false,
      editCateDialog:false,
      addCateRules:{
        cat_name:[{
          required: true,
          message: '请输入分类名',
          trigger: 'blur'
        }]
      },
      addCateForm:{
        cat_pid:0,
        cat_name:'',
        cat_level:0
      },
      parentCateList:[],
      selectedKeys:[],
      cascaderProps:{
        expandTrigger: 'hover',
        value:'cat_id',
        label:'cat_name',
        children:'children',
        checkStrictly:true
      }
    }
  },
  methods:{
    dialogClose(ref) {
      this.$refs[ref].resetFields()
      this.addCateForm.cat_level=0
      this.addCateForm.cat_pid=0
      this.selectedKeys=[]
    },
    async getCateList(){
      const {data:res} = await this.$http.get('categories',{
        params:this.queryInfo
      })
      // console.log(res);
      if (res.meta.status !== 200) return this.$Message.error("失败")
      this.categoryList = res.data.result
      this.total = res.data.total
    },
    handleSizeChange(newSize){
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange(newPage){
      this.queryInfo.pagenum = newPage
      this.getCateList()
    },
    showAddCateDialog(){
      this.getParentCateList()
this.addCateDialog = true
    },
    async getParentCateList(){
      const {data:res} = await this.$http.get('categories',{
        params:{type:2}
      })
      if (res.meta.status !== 200) return this.$Message.error("修改失败")
this.parentCateList = res.data

    },
    //级联选择器
    parentCateChange(){
      console.log(this.selectedKeys)

      if (this.selectedKeys.length>0){
        this.addCateForm.cat_pid= this.selectedKeys[this.selectedKeys.length-1]
        this.addCateForm.cat_level = this.selectedKeys.length
        return
      }else{
        this.addCateForm.cat_pid= 0
        this.addCateForm.cat_level = 0
      }
    },
    addCate(){
this.$refs.addCateDialogRef.validate(async valid =>{
  if (!valid) return

  const {data:res} = await this.$http.post('categories',this.addCateForm)
  if (res.meta.status !== 201) return this.$Message.error("添加失败")
  this.$Message.success("添加成功")
  this.getCateList()
  this.addCateDialog=false
})
    },
    removeRolesById(id){
      remove(`categories/${id}`, this.getCateList)
    },
    showEditCate(name,id){
      this.addCateForm.cat_name=name
      this.addCateForm.cat_pid=id
      this.editCateDialog=true
    },
    editCate(){
      this.$refs.editCateDialogRef.validate(async valid =>{
        if (!valid) return
        const {data:res} = await this.$http.put(`categories/${this.addCateForm.cat_pid}`,{cat_name:this.addCateForm.cat_name})
        console.log(res)
        if (res.meta.status !== 200) return this.$Message.error("编辑失败")
        this.$Message.success("编辑成功")
        this.getCateList()
        this.editCateDialog=false
      })
    }
  },
  created() {
    this.getCateList()
  },
}
</script>
<style>
.el-cascader-menu{height:300px}
.CateCascader{
  width: 100%;
}
.addCate{
  margin-bottom: 15px;
}
</style>